﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Oto_ListSalonByTinh
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            TinhThanhChinh()
            LoadTinhThanh()
            'LoadData()
            Page.Title = "Danh sách salon oto trong cả nước | OtoSaiThanh.com"
        End If
    End Sub

    Protected Sub LoadTinhThanh()
        Using Data As New VSEntities

            Dim p = (From q In Data.KhuVucs Select q).ToList
            For Each a In p
                'insert khu vuc
                Dim itm As New ListItem
                itm.Text = a.Name
                itm.Value = 0
                itm.Attributes.Add("disabled", "disabled")
                itm.Attributes.Add("class", "Khuvuc")
                ddlTinhThanh.Items.Add(itm)

                Dim _khuvucId = a.Id
                Dim k = (From q In Data.DonViHanhChinhs Where q.KhuVucId = _khuvucId Order By q.Title Ascending Select q).ToList
                For Each item In k
                    Dim itm1 As New ListItem
                    itm1.Value = item.Id
                    itm1.Text = "---" & item.Title
                    itm1.Enabled = True
                    ddlTinhThanh.Items.Add(itm1)
                Next
            Next
        End Using
    End Sub

    Protected Sub TinhThanhChinh()
        Dim curTinhThanh As New DataTable
        curTinhThanh.Columns.Add("Id")
        curTinhThanh.Columns.Add("Title")

        Using Data As New VSEntities

            Dim cookie As HttpCookie = Request.Cookies("CookieTinhThanh")
            'current choice tinh thanh
            If Not cookie Is Nothing Then
                Dim _row = curTinhThanh.NewRow
                _row(0) = cookie.Value
                Dim _curTinhTitle = (From q In Data.DonViHanhChinhs Where q.Id = cookie.Value Select q.Title).FirstOrDefault
                _row(1) = _curTinhTitle
                curTinhThanh.Rows.Add(_row)
            End If

            'toan quoc here
            Dim _rowToanQuoc = curTinhThanh.NewRow
            _rowToanQuoc(0) = 0

            _rowToanQuoc(1) = "Toàn quốc"
            curTinhThanh.Rows.Add(_rowToanQuoc)


            'main tinh thanh here
            Dim p = (From q In Data.DonViHanhChinhs Where q.MainTinhThanh = True Select New With {.Id = q.Id, .Title = q.Title}).ToList

            For Each a In p
                Dim _row1 = curTinhThanh.NewRow
                _row1(0) = a.Id
                _row1(1) = a.Title
                curTinhThanh.Rows.Add(_row1)
            Next

            With rptTinhThanhChinh
                .DataSource = curTinhThanh
                .DataBind()
            End With

        End Using

        '   Session("curTinh") = curTinhThanh



    End Sub
    Protected Sub rptTinhThanhChinh_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptTinhThanhChinh.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)

        Using Data As New VSEntities
            Dim _id As Integer = e.Item.DataItem("Id")
            Dim p As Integer = (From q In Data.Stores Where (q.TinhThanhId = _id Or _id = 0) And q.StatusId = 3 _
                                And ((q.PublishedDate <= Now() And (q.eEffectiveDate >= Now()) Or q.eEffectiveDate Is Nothing)) Select q.StoreId).Count
            hplTitle.Text = e.Item.DataItem("Title") & " (" & p.ToString & ")"
        End Using
        hplTitle.NavigateUrl = MainDomain & "danh-sach-salon.html?Id=" & e.Item.DataItem("Id")
    End Sub
    
End Class
